﻿using IGF.Utils;
using Microsoft.Web.WebView2.WinForms;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LikeProxy.Controls
{
    public partial class JSONEditorControl : UserControl
    {
        private readonly WebView2 webView;
        private bool isBrowserInitialized = false;
        private string jsonData = "";
        public JSONEditorControl()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            this.webView = new WebView2()
            {
                Dock = DockStyle.Fill,
            };
            
            this.Controls.Add(this.webView);
            InitWebview();
        }

       /* public void ParseJSON(string jsonStr)
        {
            this.jsonData = $@"app.setData(`{jsonStr}`)";
            if (isBrowserInitialized)
            {
                this.browser.GetBrowser().MainFrame.ExecuteJavaScriptAsync(jsonData);

            }
        }*/
        private async void InitWebview()
        {
            var html = Path.Combine(Application.StartupPath, @"Lib\Html\JSONEditor\index.html");
            await webView.EnsureCoreWebView2Async();
            this.webView.CoreWebView2.Navigate(html);
            //webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(jsonData);
        }
    }
}
